from __future__ import division
import imagecache
from inventory import Inventory
import constants
    
# how many pixels off a player can before being in a tile, 0 doesn't play well
# should be a small compared to constants.tile_size
movement_tolerance = 5

class Player:
    def __init__(self):
        self.money = 0
        self.x = 0
        self.y = 0
        self.direction = 'down'
        self.moving = False
        self.inventory = Inventory()
        self.player_width = constants.tile_size
        self.player_height = constants.tile_size
    
    def add_money(self, money):
        self.money += money
    
    def has_money(self, amount):
        return self.money >= amount
    
    def get_money(self):
        return self.money
    
    def get_image(self):
        return imagecache.get_image('player-' + self.direction)
    
    # move player through a link
    def transport(self, link):
        (self.x, self.y) = link.destination_coords
        return
        self.x *= constants.tile_size
        self.y *= constants.tile_size
        if self.direction == 'up':
            self.y -= constants.tile_size / 2
        elif self.direction == 'down':
            self.y += constants.tile_size / 2
        elif self.direction == 'left':
            self.x += constants.tile_size / 2
        elif self.direction == 'right':
            self.x -= constants.tile_size / 2
    
    # return the x coordinate of the tile that the player is facing
    def get_tile_x(self):
        x = self.x
        if self.direction == 'left':
            x -= movement_tolerance
        elif self.direction == 'right':
            x += constants.tile_size + movement_tolerance
        elif self.direction == 'up' or self.direction == 'down':
            x += constants.tile_size / 2
        return int(x / constants.tile_size)
    
    # return the y coordinate of the tile that the player is facing
    def get_tile_y(self):
        y = self.y
        if self.direction == 'up':
            y -= movement_tolerance
        elif self.direction == 'down':
            y += constants.tile_size + movement_tolerance
        elif self.direction == 'left' or self.direction == 'right':
            y += constants.tile_size / 2
        return int(y / constants.tile_size)
    
    # return the x coordinate of the left-most tile that the player is facing
    def get_least_tile_x(self):
        return int((self.x + movement_tolerance) / constants.tile_size)
        
    # get the y coordinate of the top-most tile that the player is facing
    def get_least_tile_y(self):
        return int((self.y + movement_tolerance) / constants.tile_size)

    # get the x coordinate of the right-most tile that the player is facing
    def get_greatest_tile_x(self):
        return int((self.x+self.player_width - movement_tolerance)/constants.tile_size)
        
    # get the y coordinate of the bottom-most tile that the player is facing
    def get_greatest_tile_y(self):
        return int((self.y+self.player_height - movement_tolerance)/constants.tile_size)
    
    def get_x(self):
        if self.direction == 'up' or self.direction == 'left':
            return self.x
        else:
            return self.x + self.player_width
    
    def get_y(self):
        if self.direction == 'up' or self.direction == 'left':
            return self.y
        else:
            return self.y + self.player_height
    
    def get_mid_x(self):
        return self.x + int(round(0.5*self.player_width))
    
    def get_mid_y(self):
        return self.y + int(round(0.5*self.player_height))
    
    def get_front(self):
        tolerance = 5
        if self.direction == 'up' or self.direction == 'down':
            return (self.get_mid_x(), self.get_y()-tolerance)            
        elif self.direction == 'down':
            return (self.get_mid_x(), self.get_y()-tolerance)
        else:
            return (self.get_x(), self.get_mid_y())
            
    
    def get_rect(self):
        return (self.x, self.y, self.player_width, self.player_height)